﻿@page "/admin/role"
@using System.Linq.Expressions;
@using BlazorComponent;
@using Masa.Blazor.Presets;
@using Microsoft.AspNetCore.Authorization;
@using ThingsGateway.Admin.Application;
@using ThingsGateway.Core.Extension

@namespace ThingsGateway.Admin.Blazor
@attribute [Authorize]
@inject UserResoures UserResoures
@attribute [RolePermission]
@inherits BaseComponentBase


<AppDataTable @ref="_datatable" TItem="SysRole" SearchItem="RolePageInput" AddItem="RoleAddInput" EditItem="RoleEditInput"
              SearchModel="@_search" IsShowSearchKey
               QueryCallAsync="QueryCallAsync" AddCallAsync="AddCallAsync"
               EditCallAsync="EditCallAsync" DeleteCallAsync="DeleteCallAsync"
                IsShowQueryButton
               IsShowAddButton=@UserResoures.IsHasButtonWithRole("RoleAdd")
               IsShowDeleteButton=@UserResoures.IsHasButtonWithRole("RoleDelete")
               IsShowEditButton=@UserResoures.IsHasButtonWithRole("RoleEdit")>

     <AddTemplate>
         @GetRenderFragment(context)


    </AddTemplate>

    <EditTemplate>
        @GetRenderFragment(context)
    </EditTemplate>

    <ItemColOperTemplate>
        <MList Dense>
            <MListItem Disabled=@(!UserResoures.IsHasButtonWithRole("RoleGrantResource")) OnClick="()=>GrantResourceAsync(context.Item)">
                <MListItemTitle Class="ml-2">@AppService.I18n.T("授权资源")</MListItemTitle>
                </MListItem>

                <MListItem Disabled=@(!UserResoures.IsHasButtonWithRole("RoleGrantUser")) OnClick="()=> GrantUserAsync(context.Item)">
                    <MListItemTitle Class="ml-2">@AppService.I18n.T("授权用户")</MListItemTitle>
                </MListItem>

                <MListItem Disabled=@(!UserResoures.IsHasButtonWithRole("RoleGrantApi")) OnClick="()=> GrantApiAsync(context.Item)">
                    <MListItemTitle Class="ml-2">@AppService.I18n.T("授权API")</MListItemTitle>
                </MListItem>

            </MList>
        </ItemColOperTemplate>

    </AppDataTable>



    @code {

    RenderFragment GetRenderFragment(RoleAddInput context)
    {
        RenderFragment renderFragment =

    @<MRow>
        <MCol Cols="12">
            <MTextField Dense Outlined HideDetails="@("auto")"
                        Style="max-width:300px"
                        Label=@(context.Description(x => x.Name)) @bind-Value=@context.Name />
        </MCol>
        <MCol Cols="12">
            <MRadioGroup @bind-Value=@context.Category Label=@(context.Description(x => x.Category))
                         Style="max-width:300px"
                         Row Dense HideDetails="@("auto")">
                <MRadio Label=@AppService.I18n.T("全局") Value=CateGoryConst.Role_GLOBAL></MRadio>
                <MRadio Label="API" Value=CateGoryConst.Role_API></MRadio>
            </MRadioGroup>

        </MCol>

        <MCol Cols="12">
            <MSlider TValue="int" ThumbLabel="@("always")" Dense
                     Style="max-width:300px;" Class="mt-8"
                     @bind-Value=@context.SortCode Label=@(context.Description(x => x.SortCode))>
                <PrependContent>
                    <MIcon OnClick="()=>context.SortCode--">
                        mdi-minus
                    </MIcon>
                </PrependContent>

                <AppendContent>
                    <MIcon OnClick="()=>context.SortCode++">
                        mdi-plus
                    </MIcon>
                </AppendContent>
            </MSlider>
        </MCol>

    </MRow>
    ;
        return renderFragment;
    }

}

